﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading;
using System.Diagnostics;
using Newtonsoft.Json;

namespace Test.SignalRHelper
{
    public class MyConnection1 : PersistentConnection
    {
        /// <summary>
        /// 连接数
        /// </summary>
        private static int connectionCount = 0;
        protected override Task OnConnected(IRequest request, string connectionId)
        {
            Interlocked.Increment(ref connectionCount);
            Debug.WriteLine("Visiteors:" + connectionCount);

            return Connection.Send(connectionId, "Welcome!");
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            //var message = JsonConvert.DeserializeObject<ChatMessage>(data);
            return Connection.Broadcast(data);
        }
        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
        {
            Interlocked.Decrement(ref connectionCount);
            Debug.WriteLine("Visitors:" + connectionCount);
            return base.OnDisconnected(request, connectionId, stopCalled);
        }
    }
}